﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Ws.dto;

namespace Ws.Util
{
    public class WsUtils
    {
        /// <summary>
        /// Get_Data接口构建参数： (xml格式) （对应的B接口内的接口名：获取监控点信息 ）
        /// </summary>
        /// <param name="fsuId">FSU</param>
        /// <param name="deviceIdList">设备</param>
        /// <param name="signalIdList">信元</param>
        public static string CreateArgs4WS(string fsuId, List<string> deviceIdList, List<string> signalIdList)
        {
            if (string.IsNullOrEmpty(fsuId))
            {
                Console.WriteLine("没有FsuId");
                return "";
            }

            if (deviceIdList == null || deviceIdList.Count <= 0)
            {
                Console.WriteLine("没有设备ID信息");
                return "";
            }

            var hasSignal = true;
            if (signalIdList == null || signalIdList.Count <= 0)
            {
                Console.WriteLine("没有信元ID信息，默认查询设备的所有信元");
                hasSignal = false;
            }

            List<Device4MonitorData> devices = new();
            for (int i = 0; i < deviceIdList.Count; i++)
            {
                Device4MonitorData item = new();
                item.IDAttribute = deviceIdList[i];
                List<string> ids = new();

                if (hasSignal)
                {
                    for (int j = 0; j < signalIdList!.Count; j++)
                    {
                        ids.Add(signalIdList[j]);
                    }
                    item.IDElement = ids;
                }
                devices.Add(item);
            }

            Info4MonitorData info = new()
            {
                FSUID = fsuId,
                DeviceList = new DeviceList4MonitorData() { Device = devices }
            };

            RequestCommon<Info4MonitorData> request = new()
            {
                Info = info,
                PK_Type = new PKType() { Name = "GET_DATA" }
            };

            // 构造完成，开始转换
            XmlSerializer x = new(typeof(RequestCommon<Info4MonitorData>));
            UTF8Writer writer = new(new StringBuilder());
            x.Serialize(writer, request);

            return writer.ToString();

        }
    }
}
